﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;

namespace SuCasa.Infrastructure
{

    public class SuCasaDbContext : DbContext
    {
        public SuCasaDbContext()
        {
            
        }

        public SuCasaDbContext(string connectionString)
            : base(connectionString)
        {
        }

        IDbSet<Course> _courses;

        public DbSet<Course> Courses
        {
            get { return (_courses ?? (_courses = Set<Course>())) as DbSet<Course>; }
        }

        IDbSet<Session> _sessions;

        public DbSet<Session> Sessions
        {
            get { return (_sessions ?? (_sessions = Set<Session>())) as DbSet<Session>; }
        }

        IDbSet<Absence> _absences;

        public DbSet<Absence> Absences
        {
            get { return (_absences ?? (_absences = Set<Absence>())) as DbSet<Absence>; }
        }

        IDbSet<Member> _members;

        public DbSet<Member> Members
        {
            get { return (_members ?? (_members = Set<Member>())) as DbSet<Member>; }
        }

        IDbSet<MemberType> _memberTypes;

        public DbSet<MemberType> MemberTypes
        {
            get { return (_memberTypes ?? (_memberTypes = Set<MemberType>())) as DbSet<MemberType>; }
        }


        IDbSet<User> _users;

        public DbSet<User> Users
        {
            get { return (DbSet<User>) (_users ?? (_users = Set<User>())); }
        }

        IDbSet<CallLog> _callLogs; 
        public IDbSet<CallLog> CallLogs
        {
            get { return _callLogs ?? (_callLogs = Set<CallLog>()); }
        }

        public virtual DbSet<T> Set<T>() where T : class
        {
            return base.Set<T>();
        }



        public virtual void Commit()
        {
            base.SaveChanges();
        }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        
            modelBuilder.Entity<User>().HasKey(x => x.UserId);
            modelBuilder.Entity<Member>().HasKey(x => x.MemberId);
            modelBuilder.Entity<Course>().HasKey(x => x.CourseId);
            modelBuilder.Entity<Course>().Property(x => x.CourseId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<CallLog>().HasKey(x => x.CallLogId);
            modelBuilder.Entity<Session>().HasKey(x => x.SessionId);
            modelBuilder.Entity<Assignment>().HasKey(x => x.Id);
            modelBuilder.Entity<StudentAssignment>().HasKey(x => new {x.AssignmentId, x.MemberId});
            modelBuilder.Entity<CallLog>().HasRequired(x => x.ReceivedByMember).WithMany().HasForeignKey(
                x => x.ReceivedByMemberId);
            //modelBuilder.Entity<Member>()
            //    .HasOptional(x => x.Parent)
            //    .WithMany(x => x.Children)
            //    .HasForeignKey(x => x.ParentMemberId);
            
            modelBuilder.Entity<StudentAssignment>().HasRequired(x=> x.Student).WithMany(x => x.Assignments).HasForeignKey(x => x.MemberId).WillCascadeOnDelete(false);
            modelBuilder.Entity<StudentAssignment>().HasRequired(x => x.Assignment).WithMany().HasForeignKey(x => x.AssignmentId).WillCascadeOnDelete(false);

           modelBuilder.Entity<Member>()
               .HasMany(u => u.Courses)
               .WithMany(r => r.Students).Map(ur =>
               {
                   ur.ToTable("MemberCourses");
                   ur.MapLeftKey("MemberId");
                   ur.MapRightKey("CourseId");
               });

            modelBuilder.Entity<Assignment>().HasRequired(x => x.Course).WithMany(x => x.Assignments).HasForeignKey(
                x => x.CourseId);

            


            //modelBuilder.Entity<User>().ToTable("Member");
            //modelBuilder.Entity<User>().Property(x => x.UserId).HasColumnName("MemberId");
            //modelBuilder.Entity<User>().Property(x => x.MemberName).HasColumnName("Member_Name");
            //modelBuilder.Entity<User>().HasMany(x => x.Addresses).WithRequired(x => x.User).HasForeignKey(x => x.UserId);


            //modelBuilder.Entity<Address>().HasKey(x => x.AddressId);
            //modelBuilder.Entity<Address>().ToTable("Address");
            //modelBuilder.Entity<Address>().HasRequired(x => x.StateRef);
            //modelBuilder.Entity<Address>().HasRequired(x => x.Country);
            //modelBuilder.Entity<Address>().Property(x => x.Address1).HasColumnName("Address_1");
            //modelBuilder.Entity<Address>().Property(x => x.Address2).HasColumnName("Address_2");
            //modelBuilder.Entity<Address>().Property(x => x.EffectiveDate).HasColumnName("EffectiveDT");
            //modelBuilder.Entity<Address>().Property(x => x.UserId).HasColumnName("MemberId");
            //modelBuilder.Entity<Address>().HasMany(x => x.PhoneNumbers).WithRequired(x => x.Address).HasForeignKey(x => x.AddressId);

            //modelBuilder.Entity<State>().HasKey(x => x.Id);
            //modelBuilder.Entity<State>().ToTable("StateCode");
            //modelBuilder.Entity<Country>().HasKey(x => x.Id);
            //modelBuilder.Entity<Country>().ToTable("CountryCode");

            //modelBuilder.Entity<PhoneNumber>().HasKey(x => x.PhoneId);
            //modelBuilder.Entity<PhoneNumber>().ToTable("Phone");
            //modelBuilder.Entity<PhoneNumber>().Property(x => x.Number).HasColumnName("Phone");
            //modelBuilder.Entity<PhoneNumber>().HasRequired(x => x.PhoneType);

            //modelBuilder.Entity<ContactType>().HasKey(x => x.Id);
            //modelBuilder.Entity<RelationshipType>().HasKey(x => x.Id).ToTable("ContactRelationship");


            //modelBuilder.Entity<PhoneType>().HasKey(x => x.Id);



        }




    }
}
